<template>
  <div class="panel-table">
    <div class="table-head">
      <span>数据列表</span>
      <div>
        <el-button type="primary" @click="dialogTableVisible = true">增加</el-button>
      </div>
    </div>
    <el-table :data="list" border style="width: 100%" :header-cell-style="{backgroundColor:'#ededed',padding:'12px 0'}">
      <el-table-column prop="id" label="编号">
      </el-table-column>
      <el-table-column prop="label" label="配置名称">
      </el-table-column>
      <el-table-column prop="keyName" label="key">
      </el-table-column>
      <el-table-column prop="value" label="配置值">
        <template #default="{row}">
          <el-tooltip class="text-tlp" effect="dark" :content="row.value" placement="top" popper-class="text-tlp" :show-after="300">
            <div class="ellipsis">
              {{row.value}}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="updatedAt" label="更新时间">
      </el-table-column>
      <el-table-column label="操作" align="center" width="120px">
        <template #default="{row}">
          <el-button @click="editHandle(row)" type="primary" plain size="small">编辑</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-dialog title="详情" v-model="dialogTableVisible" @close="resetData">
      <el-form :model="form" label-width="80px">
        <el-form-item label="配置名称">
          <el-input v-model="form.label"></el-input>
        </el-form-item>
        <el-form-item label="key">
          <el-input v-model="form.keyName" :disabled="!!form.id"></el-input>
        </el-form-item>
        <el-form-item label="配置值">
          <el-input v-model="form.value" type="textarea" :rows="4"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogTableVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>


</template>

<script>
import { getConfigList, editConfigList } from "@/api/config";
import { ElMessage } from "element-plus";
export default {
  name: "KeyConfigList",
  data() {
    return {
      list: [],
      form: {
        label: "",
        keyName: "",
        value: "",
      },
      dialogTableVisible: false,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getConfigList().then((res) => {
        if (res.code == 0) {
          this.list = res.data;
        }
      });
    },
    editHandle(row) {
      this.dialogTableVisible = true;
      this.form = {
        id: row.id,
        label: row.label,
        keyName: row.keyName,
        value: row.value,
      };
    },
    onSubmit() {
      editConfigList(this.form).then((res) => {
        if (res.code == 0) {
          this.dialogTableVisible = false;
          this.init();
          ElMessage.success("成功");
        }
      });
    },
    resetData() {
      this.form = {
        label: "",
        keyName: "",
        value: "",
      };
    },
  },
};
</script>

<style scoped lang="scss">
@import "@/styles/tablePage.scss";
.ellipsis {
  max-width: 300px;
  margin-left: 16px;
  font-size: 14px;
  line-height: 18px;
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
<style>
.text-tlp {
  max-width: 700px;
  line-height: 20px;
}
</style>
